package work;

import java.util.concurrent.TimeUnit;

public class Work1 {
    public static void main2(String[] args) throws InterruptedException {
        Thread[] threads=new Thread[20];
        for (int i = 0; i < threads.length; i++) {
            final int n=i;
                threads[i] = new Thread(() -> {
                    System.out.println(n);
                });
                threads[i].start();
                threads[i].join();
        }
        threads[19].join();
        System.out.println("OK");
    }

    public static void main(String[] args) throws InterruptedException {
        Thread[] threads=new Thread[20];
        for (int i = 0; i < threads.length; i++) {
            final int n=i;
            threads[i] =new Thread(()->{
                System.out.println(n);
            });
        }
        for (Thread t:threads
             ) {
            t.start();
        }
        for (Thread t:threads
             ) {
            t.join();
        }
        System.out.println("OK");
    }
    public static void main1(String[] args) throws InterruptedException {
        Thread[] threads = new Thread[20];
        for(int i=0; i<20; i++){
            final int n = i;
            threads[i] = new Thread(new Runnable() {
                @Override
                public void run() {//内部类使用外部的变量，必须是final修饰
                    System.out.println(n);
                }
            });
        }
        for(Thread t : threads){
            t.start();
        }
        for(Thread t : threads){//同时执行20个线程，再等待所有线程执行完毕
            t.join();
        }
        System.out.println("OK");
    }
}

